package cms.api.solr.index.controller;

import cms.api.framework.search.IndexService;
import cms.api.framework.search.QueryParams;
import cms.api.solr.index.model.SolrDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Solr索引服务控制器
 *
 * @author heyingcheng
 * @email heyingcheng@ctvit.com.cn
 * @date 2019/6/30 8:02
 */
@RestController
@RequestMapping("index")
public class IndexController {

    @Autowired
    private IndexService indexService;

    @PostMapping
    public Object createIndex(@RequestParam String index) {
       return indexService.createIndex(index);
    }

    @DeleteMapping
    public Object deleteIndex(@RequestParam String index) {
        return indexService.deleteIndex(index);
    }

    @GetMapping
    public Object getIndex(@RequestParam String index) {
        return indexService.isIndexExist(index);
    }

    @DeleteMapping("document")
    public Object deleteDocumentById(@RequestParam String index, @RequestParam String id) {
        return indexService.deleteDocumentById(index, null, id);
    }

    @DeleteMapping("document/query")
    public Object deleteDocumentByQuery(@RequestBody QueryParams queryParams) {
        return indexService.deleteDocumentByQuery(queryParams.getIndex(), null, queryParams);
    }

    @GetMapping("document/fetch")
    public Object fetchDocumentById(@RequestParam String index, @RequestParam String id, @RequestParam(value = "rtnField", required = false) String[] rtnFields) {
        return indexService.fetchDocumentById(index, null, id, rtnFields);
    }

    @GetMapping("document/query/string")
    public Object search(@RequestParam String index, @RequestParam String queryString) {
        return indexService.search(index, null, queryString);
    }

    @PostMapping("document")
    public Object addDocument(@RequestParam String index, @RequestBody SolrDocument solrDocument) {
        return indexService.addDocument(index, null, solrDocument);
    }

}
